﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using System.Web.UI;
using System.Data;
using ASPNETVN.PORTAL.Modules.DOCUMENT.Components;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using System.Web;
using ASPNETVN.PORTAL.Components.Share;
using System.Collections;
using System.Web.UI.WebControls;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT
{
    public enum ModulePages
    {
        Category,
        Item,
        ItemWithMultipleCategories,
        Detail,
        CategoryManager,
        Search,
        SearchWithMultipleCategories,
        ItemManager
    }

    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            // Define an Array List
            ArrayList list1 = new ArrayList();

            // Sắp xếp theo cột.
            list1.Add(new ListItem("10 records per a page", "10"));
            list1.Add(new ListItem("20 records per a page", "20"));
            list1.Add(new ListItem("50 records per a page", "50"));
            list1.Add(new ListItem("100 records per a page", "100"));

            ModuleSettingItem setting = new ModuleSettingItem(new CustomListDataType(list1, "Text", "Value"));
            setting.Name = "Page Size";
            setting.Value = "10";
            setting.Description = "Page Size";
            setting.SortOrder = 1;
            this.BaseSettings.Add("PageSize", setting);

            // Yêu cầu đăng nhập trước khi download.
            setting = new ModuleSettingItem(new BooleanDataType(false));
            setting.Name = "Require authentication";
            setting.Description = "Require authentication before download";
            setting.SortOrder = 2;
            this.BaseSettings.Add("RequireAuthentication", setting);

            // Cách thức lưu trữ file upload.
            ArrayList listStorageType = new ArrayList();
            listStorageType.Add(new ListItem("Database", "DATABASE"));
            listStorageType.Add(new ListItem("Link", "LINK"));
            listStorageType.Add(new ListItem("File", "FILE"));

            setting = new ModuleSettingItem(new CustomListDataType(listStorageType, "Text", "Value"));
            setting.Name = "Storage Type";
            setting.Description = "Storage Type";
            setting.SortOrder = 3;
            this.BaseSettings.Add("StorageType", setting);

            setting = new ModuleSettingItem(new BooleanDataType(false));
            setting.Name = "Multiple Categories per one Item";
            setting.Description = "Multiple Category per one Item";
            setting.SortOrder = 4;
            this.BaseSettings.Add("MultipleCategories", setting);
        }

        private void LoadChildControl()
        {
            const string basePath = "~/Modules/ASPNETVN.PORTAL.Modules.DOCUMENT/";
            const string pageQuery = "page";
            string pageName = Request.QueryString[pageQuery];

            ModulePages page;
            if (pageName == null)
            {
                if (Convert.ToBoolean(this.Settings["MultipleCategories"].ToString()))
                {
                    page = ModulePages.ItemWithMultipleCategories;
                }
                else
                {
                    page = ModulePages.Item;
                }
            }
            else
            {
                page = (ModulePages)Enum.Parse(typeof(ModulePages), Request.QueryString[pageQuery], true);
            }

            string src = string.Format("{0}Controls/{1}.ascx", basePath, page);
            ModulePage ctl = (ModulePage)this.LoadControl(src);
            ctl.ParentControl = this;
            ContentPane.Controls.Add(ctl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            this.LoadChildControl();
            if (Convert.ToBoolean(this.Settings["MultipleCategories"].ToString()))
            {
                this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditItemWithMultipleCategories");
            }
            else
            {
                this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditItem");
            }
        }
    }
}